package cn.sczhyt.modules.app.controller;

import cn.sczhyt.common.utils.PageUtils;
import cn.sczhyt.common.utils.R;
import cn.sczhyt.common.utils.UploadFileUtils;
import cn.sczhyt.modules.app.annotation.Login;
import cn.sczhyt.modules.app.utils.FileReadOnlineUtils;
import cn.sczhyt.modules.szhy.entity.MeetingNoticeEntity;
import cn.sczhyt.modules.szhy.entity.VO.MeetingNoticeEntitySaveVO;
import cn.sczhyt.modules.szhy.service.MeetingNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;


/**
 * 通知表
 *
 * @author luojialin
 * @email 78357966@qq.com
 * @date 2021-04-02 10:15:15
 */
@Api(tags = "App消息模块")
@RestController
@RequestMapping("app/meetingnotice")
public class AppMeetingNoticeController {
    private final BASE64Decoder decoder = new BASE64Decoder();
    @Autowired
    private MeetingNoticeService meetingNoticeService;
    /**
     * 查看信息内容
     */
    @Login
    @ApiOperation("查看信息内容详情")
    @RequestMapping("/info/{noticeId}")
    public R info(@PathVariable("noticeId") String noticeId){

		MeetingNoticeEntity meetingNotice = meetingNoticeService.getById(noticeId);

        return R.ok().put("meetingNotice", meetingNotice);
    }

    /**
     * 通知公告的文件预览
     */
    @ApiOperation("通知公告的文件预览")
    @RequestMapping("/file/{noticeId}/{fileName}")
    public R file(HttpServletResponse response,
                  @PathVariable("noticeId") String noticeId,
                  @PathVariable(value = "fileName") String fileName
                  ) throws IOException {
        MeetingNoticeEntity meetingNotice = meetingNoticeService.getById(noticeId);
        String path = meetingNotice.getNoticeEnclosurePath();
        String url = null;
        JSONArray jsonArray = JSONArray.fromObject(path);
        for (Object o : jsonArray) {
            JSONObject jsonObject = JSONObject.fromObject(o);
            if (jsonObject.get("name").equals(fileName)){
                url = (String) jsonObject.get("url");
            }
        }

        FileReadOnlineUtils.fileReadOnline(response,url,fileName,"pdf");

        return R.ok();
    }
}
